package jude.java.money.service;

import java.util.List;

import jude.java.money.beans.FtpSite;
import jude.java.money.beans.WebSite;

import org.hibernate.type.BooleanType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.Type;

public class HeiService extends BaseService {
	
	public int getPingFtpSiteCount() {
		return count(FtpSite.class, "ignored=?", new Object[] {Boolean.FALSE}, null);
	}
	
	
	public List<FtpSite> listPrFtpSites(int pageSize, int pageIndex) {
		return query(FtpSite.class, "ignored=0 and (pr=-2)", pageSize, pageIndex, null, null);
	}
	
	/**
	 * 所有站点
	 * */
	public List<FtpSite> listAllPingFtpSites(int pageSize, int pageIndex) {
		return query(FtpSite.class, null, pageSize, pageIndex, null, null);
	}
	/**
	 * 所有未忽略的站点
	 */
	public List<FtpSite> listNotIgnoredPingFtpSites(int pageSize, int pageIndex) {
		return query(FtpSite.class, "ignored=?", pageSize, pageIndex, new Object[] {Boolean.FALSE}, null);
	}
	/**
	 * 所有可连接的站点(不含忽略的和UnknownHostException的站点)
	 */
	public List<FtpSite> listPingFtpSites(int pageSize, int pageIndex) {
		return query(FtpSite.class, "ignored=? and (error is null or error!=?)", pageSize, pageIndex, new Object[] {Boolean.FALSE, "UnknownHostException"}, null);
	}
	/**
	 * 所有未登录的站点(不含忽略的和UnknownHostException的站点), 用于重试
	 * */
	public List<FtpSite> listNotLoginedPingFtpSites(int pageSize, int pageIndex) {
		return query(FtpSite.class, "ignored=? and logined=? and (error is null or error!=?)", pageSize, pageIndex, new Object[] {Boolean.FALSE, Integer.valueOf(0), "UnknownHostException"}, null);
	}
	
	public void resetFtpSiteTimeout() {
		update(FtpSite.class, "set timeout=?", new Object[] {Boolean.FALSE});
	}
	
	public void updateFtpSiteTimeout() {
		update(FtpSite.class, "set overtime=overtime+1,timeout=? where timeout=?", new Object[] {false, true});
	}

	public List<FtpSite> listHeiFtpSites() {
		StringBuilder search = new StringBuilder();
//		search.append("host='aikejiaju.com' and ");
		search.append("ignored=? and logined>? order by updateTime asc, overtime asc, id asc");
		Object[] values = new Object[] {Boolean.FALSE, Integer.valueOf(0)};
		Type[] types = new Type[] {new BooleanType(), new IntegerType()};
		return query(FtpSite.class, search.toString(), 3000, 0, values, types);
	}
	
	public List<FtpSite> listHeiFtpSites(int pageSize, int pageIndex, boolean reverse) {
		StringBuilder search = new StringBuilder();
		search.append("ignored=? and logined>?");
		if (reverse)
			search.append(" order by updateTime asc");
		Object[] values = new Object[] {Boolean.FALSE, Integer.valueOf(0)};
		Type[] types = new Type[] {new BooleanType(), new IntegerType()};
		return query(FtpSite.class, search.toString(), pageSize, pageIndex, values, types);
	}
	
	public int countHeiFtpSite() {
		StringBuilder search = new StringBuilder();
		search.append("ignored=? and logined>?");
		Object[] values = new Object[] {Boolean.FALSE, Integer.valueOf(0)};
		Type[] types = new Type[] {new BooleanType(), new IntegerType()};
		return count(FtpSite.class, search.toString(), values, types);
	}
	
	public List<FtpSite> listTimeoutFtpSites() {
		StringBuilder search = new StringBuilder();
		search.append("timeout=?");
		Object[] values = new Object[] {Boolean.TRUE};
		Type[] types = new Type[] {new BooleanType()};
		return query(FtpSite.class, search.toString(), 1000, 0, values, types);
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getWebSiteGroups() {
		StringBuilder sb = new StringBuilder("select distinct o.group from ");
		sb.append(WebSite.class.getName()).append(" as o");
		return (List<String>) dao.query(sb.toString());
	}
	
	@SuppressWarnings("deprecation")
	public List<WebSite> listWebSites(){
		return dao.query(WebSite.class, "order by group");
	}
}
